package genCode;

import genCode.generator.AdminGenerator;
import genCode.generator.BaseActionGenerator;
import genCode.generator.DaoGenerator;
import genCode.generator.ServiceGenerator;
import genCode.generator.impl.BaseActionGeneratorImpl;
import genCode.generator.impl.DaoGeneratorImpl;
import genCode.generator.impl.ServiceGeneratorImpl;
import genCode.test.BaseTest;
import genCode.util.GenUtil;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class GenerateCode extends BaseTest {
	
	@Autowired
	AdminGenerator adminGenerator;
	
	/**
	 * 请注入数据连接
	 */
	@Autowired
	@Qualifier("local-hibernateTemplate")
	HibernateTemplate targetHibernateTemplate;

	/**
	 * 请配置生成的数据模型包名
	 */
	public static final String PACKAGE="com.tplanet.model";
	/**
	 * 请配置数据层使用的数据连接
	 * 如想使用默认数据连接:TARGET_HIBERNATETEMPLATE=""
	 */
	public static final String TARGET_HIBERNATETEMPLATE="";
	/**
	 * 数据层是否覆盖原有代码
	 */
	public static final boolean DAO_ISCOVER=false;
	/**
	 * 业务层是否覆盖原有代码
	 */
	public static final boolean SERIVCE_ISCOVER=false;
	/**
	 * 基础Action是否覆盖原有代码
	 */
	public static final boolean BASEACTION_ISCOVER=true;
	/**
	 * Action层是否覆盖原有代码
	 */
	public static final boolean ACTION_ISCOVER=false;
	public String currentPackageName=StringUtils.substringAfterLast(StringUtils.substringBeforeLast(PACKAGE, "."), ".");

	
	
	@Test
	public void testGenerateDao() {

		try {
			List<Class> modelList=GenUtil.getPackageClass(PACKAGE);
			DaoGenerator daoGenerator=new DaoGeneratorImpl();
			for (Class modelClass : modelList) {
				daoGenerator.genAll(modelClass,TARGET_HIBERNATETEMPLATE,DAO_ISCOVER);
			}
		} catch (ClassNotFoundException e) {
			System.out.println("生成数据层出错");
		}
	}
	
	@Test
	public void testGenerateSerivce() {
		try {
			List<Class> modelList=GenUtil.getPackageClass(PACKAGE);
			ServiceGenerator serviceGenerator=new ServiceGeneratorImpl();
			for (Class modelClass : modelList) {
				serviceGenerator.genAll(modelClass, SERIVCE_ISCOVER);
			}
		} catch (ClassNotFoundException e) {
			System.out.println("生成服务层出错");
		}
	}
	
	@Test
	public void testGenerateBaseAction() {
		try {
			List<Class> modelList=GenUtil.getPackageClass(PACKAGE);
			BaseActionGenerator baseActionGenerator=new BaseActionGeneratorImpl();
			baseActionGenerator.genBaseAction(modelList, BASEACTION_ISCOVER);
		} catch (ClassNotFoundException e) {
			System.out.println("生成基础控制层出错");
		}
	}
	
	@Test
	/**
	 * 生成页面与action层
	 */
	public void testGenerateAction() {
		try {
			List<Class> modelList=GenUtil.getPackageClass(PACKAGE);
			List<String> modelObjectList=new ArrayList<String>();
			for (Class class1 : modelList) {
				adminGenerator.genAdmin(class1,targetHibernateTemplate, ACTION_ISCOVER);
				String modelObject=StringUtils.uncapitalize(class1.getSimpleName());
				modelObjectList.add(modelObject);
			}
			adminGenerator.genIndextJsp(modelObjectList,currentPackageName,ACTION_ISCOVER);
		} catch (ClassNotFoundException e) {
			System.out.println("生成页面控制层出错");
		}
	}

}
